import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'
import {notification} from "ant-design-vue";
const routes = [
    {
    path: '/login',
    component: () => import('../views/Login.vue')
  },{
    path: '/',
    component: () => import('../views/Main.vue'),
        meta: {
            loginRequire: true
        },
        children: [{
            path: 'welcome',
            component: () => import('../views/main/member/Welcome.vue'),
        }, {
            path: 'passenger',
            component: () => import('../views/main/member/Passenger.vue'),
        }, {
            path: 'ticket',
            component: () => import('../views/main/member/ticket')
        }]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
    if (to.matched.some(function (item) {
        console.log(item, '是否需要登录校验：', item.meta.loginRequire || false)
        return item.meta.loginRequire
    })) {
        const member = store.state.member
        console.log('页面登录校验开始：', member)
        if (!member.token) {
            console.log('用户未登录或登录超时！')
            notification.error({description: '未登录或者登录超时！'})
            next('/login')
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router
